package com.xinwei.springboot.controller;

import com.xinwei.springboot.common.response.PageVO;
import com.xinwei.springboot.common.response.Result;
import com.xinwei.springboot.domain.Student;
import com.xinwei.springboot.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("api/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 增加学生信息  
     * @param student
     * 3g@return
     */
    @PostMapping("")
    public Result<Void>addStudent(Student student){
        studentService.save(student);
        return Result.success();
    }

    /**
     * 删除学生信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteStudent(@RequestParam Integer id){
        studentService.deleteStudentById(id);
        return Result.success();
    }

    /**
     *修改学生信息
     * @param student
     * @return
     */
    @PutMapping("")
    public Result<Void> updateStudent(Student student){
        studentService.save(student);
        return Result.success();
    }

    /**
     * 查找学生信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Void> findStudentById(@RequestParam Integer id){
        Student student=studentService.findStudentById(id);
        if (student == null) {
            return Result.failure();
        }
        else return Result.success();
    }
    /**
     * 分页查询学生信息
     */
    @GetMapping("/findAll")
    public Result<PageVO<Student>> listStudentPage(@RequestParam(defaultValue = "1") Integer page,
                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Student> list = studentService.findByPage(page - 1, pageSize);
        PageVO<Student> pageVO = PageVO.getPageVO(list);
        return Result.success(pageVO);
    }
}


